<template>
  <div class="normal-material">
    <el-tabs v-model="activeTab" @tab-click="tabClick">
      <el-tab-pane v-for="(item, index) in tabList" :key="index" :label="item.label" :name="item.value">
        <div class="page-content">
          <div v-loading="pageLoading" class="content-group">
            <div class="group-main-title">
              <div class="title-name" @click="handleClickAll">
                <i :class="['el-icon-caret-bottom', iconRotate ? 'icon-rotate-90' : 'icon-rotate-0']" style="color:#ccc"></i>全部
              </div>
              <div class="title-btn" @click="addOrUpdateGroup('ADD')">
                <i class="el-icon-circle-plus-outline"></i>添加
              </div>
            </div>
            <div :class="iconRotate ? 'group-name' : 'group-name-unfold'">
              <div v-for="item in groupList" :key="item.id" :class="['group-title', 'group-item', { 'group-item-active': currentGroupIndex === item.id }]" @click="switchGroup(item)">
                <div class="item-name">{{ item.name }}</div>
                <div>
                  <el-dropdown>
                    <i class="el-icon-more"></i>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item>
                        <el-button type="text" @click="addOrUpdateGroup('EDIT', item)">编辑
                        </el-button>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <el-button type="text" @click="deleteGroup(item)">删除</el-button>
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </div>
              </div>
            </div>
          </div>
          <div class="content-table">
            <NormalForm :isShowCard="false" :operateForm="queryForm" :formList="formList" @specialSearch="specialSearch" @handleQuery="handleQuery" @handleReset="handleReset">
              <template slot="more-btn">
                <slot name="material-btn"></slot>
              </template>
            </NormalForm>
            <slot :name="item.slotName"></slot>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!-- 新建或修改分组 -->
    <el-dialog :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" :title="`${currentOperate === 'ADD' ? '新建' : '编辑'}分组`" :visible.sync="groupVisible" width="520px">
      <el-form ref="groupForm" :model="groupForm" :rules="groupRules">
        <el-form-item label="请输入分组名称" label-width="130px" prop="name">
          <el-input v-model="groupForm.name" clearable maxlength="32"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleGroupDialogClose">取 消</el-button>
        <el-button type="primary" @click="onAddOrUpdateGroup">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 删除分组 -->
    <el-dialog title="删除确认" :visible.sync="delVisible" width="400px" :show-close="false" :close-on-click-modal="false">
      <div>确认删除该分组？</div>
      <div slot="footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureDel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { meterialDoneApi } from '@/utils/meterialDoneApi';

export default {
  name: 'NormalMaterial',
  props: {
    currentTab: {
      type: String,
      default: '0'
    },
    // 用于判断用什么接口操作分组
    pagePwd: {
      type: String,
      default: ''
    },
    tabList: {
      type: Array,
      default: () => []
    },
    levelType: {
      type: Number,
      default: 3 // 活码都是3，暂时写死
    },
    queryForm: {
      type: Object,
      default: () => {}
    },
    formList: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      activeTab: '0',
      pageLoading: false,
      groupList: [],
      iconRotate: false,
      currentGroupIndex: null,
      // 操作分组
      currentOperate: '',
      currentGroup: null,
      groupVisible: false,
      groupForm: {
        name: '',
      },
      groupRules: {
        name: [{ required: true, message: '请输入分组名称', trigger: 'blur' }],
      },
      delVisible: false,
      delGroupId: null,
      mediaTypeOpt: {
        'File': 3,    // 文件库
        'Article': 4,  // 文章库
        'Poster': 5,   // 海报库
        'Audio': 7,    // 多媒体
        'Words': 8,    // 话术库
        'Form': 10,    // 问卷库
        'Link': 11,    // 链接库
      }
    }
  },
  mounted() {
    this.activeTab = this.currentTab || '0'
    this.handleGetGroupList();
  },
  methods: {
    // tab切换
    tabClick(val) {
      this.handleGetGroupList();
      this.currentGroup = '';
      this.currentGroupIndex = '';
      this.iconRotate = false;
      this.$emit('tabClick', val.index);
    },
    // 全部分组
    handleClickAll() {
      this.iconRotate = !this.iconRotate;
      if (this.iconRotate) {
        this.currentGroup = '';
        this.currentGroupIndex = '';
        this.$emit('handleQuery', '', 1);
      } else {
        this.handleGetGroupList();
      }
    },
    // 删除分组
    deleteGroup(data) {
      this.delVisible = true
      this.delGroupId = data.id
    },
    sureDel() {
      meterialDoneApi(this.pagePwd, this.activeTab, 'DEL', this.delGroupId).then(res => {
        if (res == 'SUCCESS') {
          this.handleGetGroupList()
          this.$message.success('分组删除成功!')
        }
        this.delVisible = false
      })
    },
    // 选择分组
    switchGroup(data) {
      if (this.currentGroupIndex === data.id) return
      this.currentGroupIndex = data.id
      this.currentGroup = data
      this.$emit('handleQuery', this.currentGroupIndex);
    },
    // 查询分组列表
    handleGetGroupList() {
      this.pageLoading = true;
      const params = {
        levelType: +this.activeTab,
        mediaType: this.mediaTypeOpt[this.pagePwd]
      }
      meterialDoneApi(this.pagePwd, this.activeTab, 'QUERY', params).then(res => {
        if (res !== 'ERROR') {
          this.groupList = res;
        }
        this.pageLoading = false;
      })
    },
    // 添加、编辑分组
    addOrUpdateGroup(type, data) {
      this.currentOperate = type;
      this.currentGroup = data || null;
      if (data) this.groupForm.name = data.name
      this.groupVisible = true;
    },
    // 取消保存分组
    handleGroupDialogClose() {
      this.groupVisible = false
      this.$refs.groupForm.resetFields()
    },
    // 保存分组
    onAddOrUpdateGroup() {
      this.$refs.groupForm.validate(async (valid) => {
        if (valid) {
          const params = {
            id: this.currentGroup ? this.currentGroup.id : null,
            levelType: +this.activeTab,
            mediaType: this.mediaTypeOpt[this.pagePwd],
            name: this.groupForm.name
          }
          meterialDoneApi(this.pagePwd, this.activeTab, this.currentOperate, params).then(res => {
            if (res !== 'ERROR') {
              this.groupVisible = false
              this.handleGetGroupList()
              this.$message.success('分组保存成功!')
              this.groupForm.name = ''
            }
          })
        } else {
          return false
        }
      })
    },
    handleQuery() {
      this.$emit('handleQuery', this.currentGroupIndex, 1)
    },
    handleReset() {
      this.$emit('handleReset', this.currentGroupIndex, 1)
    },
    specialSearch() {
      this.$emit('handleQuery', this.currentGroupIndex, 1)
    },
  }
}
</script>

<style lang="scss" scoped>
.normal-material {
  height: 100%;
  .page-content {
    height: 100%;
    display: flex;
    .content-group {
      width: 200px;
      border-right: 1px solid #f1f1f1;
      padding-right: 12px;
      font-size: 12px;
      .group-main-title{
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        cursor: pointer;
        .icon-rotate-90 {
          transition: transform 0.2s, -webkit-transform 0.2s;
          transform: rotateZ(-90deg);
          color: #C0C4CC;
        }
        .icon-rotate-0 {
          transition: transform 0.2s, -webkit-transform 0.2s;
          transform: rotate(0deg);
          color: #C0C4CC;
        }
        .title-name {
          flex: 1;
        }
        .title-btn {
          color: #082aff;

          &:hover {
            opacity: 0.7;
          }
        }
        i {
          margin-right: 5px;
        }
      }
      .group-title {
        display: flex;
        justify-content: space-between;
        cursor: pointer;
        padding: 6px 10px;
      }

      .group-name {
        max-height: 0;
        overflow: hidden;
      }
      .group-name-unfold {
        max-height: calc(100% - 30px);
        transition: max-height 0.3s;
        overflow: scroll;
        &::-webkit-scrollbar {
          width: 0;
        }
        -ms-overflow-style: none;
        overflow: -moz-scrollbars-none;
      }

      .group-item-active {
        background-color: #fff2ee;
      }
    }

    .content-table {
      width: calc(100% - 200px);
      padding-left: 16px;
    }
  }
  .el-tabs{
    height: 100%;
    ::v-deep.el-tabs__content{
      height: calc(100% - 50px);
      .el-tab-pane{
        height: 100%;
      }
    }
  }
}
</style>
